package problem;

import java.util.Arrays;

/**
 * @author RunningShrimp
 * @date 2021/6/13  13:47
 * @see <a href="https://leetcode-cn.com/problems/merge-sorted-array/">88. 合并两个有序数组</a>
 */
public class MergeSortedArray {
    public void merge(int[] nums1, int m, int[] nums2, int n) {
        if (n == 0) {
            return;
        }

        for (int i = m; i < nums1.length; i++) {
            int index = nums1.length - i-1;
            nums1[i] = nums2[index];
        }
        Arrays.sort(nums1);
    }

    /**
     * 利用归并排序
     * @param nums1
     * @param m
     * @param nums2
     * @param n
     */
    public void merge2(int[] nums1, int m, int[] nums2, int n) {
        if (n == 0) {
            return;
        }

        for (int i = m; i < nums1.length; i++) {
            int index = nums1.length - i-1;
            nums1[i] = nums2[index];
        }
        Arrays.sort(nums1);
    }

    public static void main(String[] args) {
        int[] arr = new int[]{1, 2, 3, 0, 0, 0};
        new MergeSortedArray().merge(arr, 3, new int[]{2, 5, 6}, 3);
        System.out.println(Arrays.toString(arr));
    }
}
